﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.Data
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid

Public Class MH_MuaHangNuocNgoai1
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Dim DoanhThu, PhanTramThue, TienThue, TongCong As Decimal
    Dim MaHang, TenHang, DVT, LoHang As String
    Dim TaiKhoanNo, TaiKhoanCo, TaiKhoanThue As String
    Dim HSD As Date
    Dim m_MaPhieu As String
    Public Sub setMa(ByVal ma As String)
        Me.m_MaPhieu = ma
    End Sub
    Private Sub setReadonlyControl(ByVal b As Boolean)
        setReadonly(Me.layoutControl, b)
        If b Then
            Me.IGridViewHangHoa.OptionsBehavior.Editable = False
            Me.IGridViewThue.OptionsBehavior.Editable = False
            Me.IGridViewChatLuong.OptionsBehavior.Editable = False
            Me.IGridViewDsBanHang.setLock(False)
        Else
            Me.IGridViewHangHoa.OptionsBehavior.Editable = True
            Me.IGridViewThue.OptionsBehavior.Editable = True
            Me.IGridViewChatLuong.OptionsBehavior.Editable = True
            Me.IGridViewDsBanHang.setLock(True)
        End If
        If b Then 'trong trạng thái readonly  thì không cho nhập và dòng trống thêm ở cuối
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else  'cho thêm dòng trống ở cuối (khác form đơn - bỏ dòng : And State = 1)
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
            Me.BarButtonItemTaoMoi.Enabled = False
            Me.BarButtonItemFirst.Enabled = False
            Me.BarButtonITemPrevious.Enabled = False
            Me.BarButtonItemNext.Enabled = False
            Me.BarButtonItemLast.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
            Me.BarButtonItemTaoMoi.Enabled = True
            Me.BarButtonItemFirst.Enabled = True
            Me.BarButtonITemPrevious.Enabled = True
            Me.BarButtonItemNext.Enabled = True
            Me.BarButtonItemLast.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.KHHangHoaTableAdapter.Fill(Me.PCV_FISHDataSet.KHHangHoa)
        Me.KHHangHoaChiTietTableAdapter.Fill(Me.PCV_FISHDataSet.KHHangHoaChiTiet)
        Me.DmkhachhangTableAdapter.FillByMaTenDiaChi(Me.PCV_FISHDataSet.dmkhachhang)
        Me.DmnghiepvuTableAdapter.Fill(Me.PCV_FISHDataSet.dmnghiepvu)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.DmnguonvonTableAdapter.Fill(Me.PCV_FISHDataSet.dmnguonvon)
        Me.DmkheuocvayTableAdapter.Fill(Me.PCV_FISHDataSet.dmkheuocvay)
        Me.DmkhoanmucphiTableAdapter.Fill(Me.PCV_FISHDataSet.dmkhoanmucphi)
        Me.DmnganhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmnganhang)
        Me.DmhanghoaTableAdapter.FillByMaTenDonViTinh(Me.PCV_FISHDataSet.dmhanghoa)
        Me.KHThueTableAdapter.Fill(Me.PCV_FISHDataSet.KHThue)
        Me.DmkhoTableAdapter.Fill(Me.PCV_FISHDataSet.dmkho)
        Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        Me.GetDMNghiepVu_MuaHangTrongNuocTableAdapter.Fill(Me.PCV_FISHDataSet.getDMNghiepVu_MuaHangTrongNuoc)
        Me.DmphongbanTableAdapter.Fill(Me.PCV_FISHDataSet.dmphongban)
        Me.KHQuanLyViTriHangTableAdapter.Fill(Me.PCV_FISHDataSet.KHQuanLyViTriHang)
        Me.GetPhanLoai_ThanhToanTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_ThanhToan)
        setReadonlyControl(True)
        Me.TC_TienIGridControl.ForceInitialize()
        Me.KHHangHoaBindingSource.MoveLast()
        SplashScreenManager.CloseForm()
    End Sub

    Private Sub MH_MuaHangNuocNgoai1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        If Me.m_MaPhieu IsNot Nothing Then
            Dim position As Integer = Me.KHHangHoaBindingSource.Find("ma", Me.m_MaPhieu)
            Me.KHHangHoaBindingSource.Position = position
        End If
    End Sub
    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonlyControl(False)
        'Mã //set ngầm
        Me.ManghiepvuISearchLookupEdit.Focus()
        Me.ManghiepvuISearchLookupEdit.ShowPopup()
        Dim row As DataRowView = CType(KHHangHoaBindingSource.AddNew(), DataRowView)
        Me.Ma = "MHNN" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        Me.LoaitienISearchLookupEdit.EditValue = "VND"
        Me.TienvndITextEdit.EditValue = "0"
        Me.NguyenteITextEdit.EditValue = "0"
        Me.TinhtrangISearchLookupEdit.EditValue = "NL"
        'mặc định
        Me.NgaychungtuIDateEdit.EditValue = Today
        Me.NgaythanhtoanIDateEdit.EditValue = Today
        Me.TygiaITextEdit.EditValue = 1
        Me.State = 1
    End Sub

    Private Sub MakhachhangSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs)
       
    End Sub
    Private Sub setCongNoPhaiThu() 'xử lý công nợ phải thu 
        Dim sochungtu As String = SochungtuITextEdit.EditValue
        Dim ma As String = Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma").ToString
        Dim ngaychungtu As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
        Dim madoituong As String = Me.MadoituongISearchLookupEdit.EditValue
        Dim tendoituong As String = Me.MadoituongISearchLookupEdit.Text
        Dim diachidoituong As String = Me.DiachiITextEdit.EditValue
        Dim sohoadon As String = Me.SohoadonITextEdit.EditValue
        Dim ngayhoadon As Date = CDate(Me.NgayhoadonIDateEdit.EditValue)
        Dim hanthanhtoan As Date = CDate(NgaythanhtoanIDateEdit.EditValue)
        Dim loaitien As String = Me.LoaitienISearchLookupEdit.EditValue
        Dim tygia As Decimal = CDec(Me.TygiaITextEdit.EditValue)
        Dim nguyente As Decimal = CDec(Me.NguyenteITextEdit.EditValue)
        Dim thanhtienvnd As Decimal = CDec(Me.TienvndITextEdit.EditValue)
        Dim taikhoanno As String = Me.PCV_FISHDataSet.getDMNghiepVu_MuaHangTrongNuoc.Rows(Me.GetDMNghiepVuMuaHangTrongNuocBindingSource.Find("ma", Me.ManghiepvuISearchLookupEdit.EditValue)).Item("taikhoanno").ToString
        Dim kyketoan As String = CDate(NgaychungtuIDateEdit.EditValue).Year & Format(CDate(NgaychungtuIDateEdit.EditValue).Month, "00")
        'Me.GetTCCongNoPhaiThu_isTonTaiTableAdapter.Fill(Me.PCV_FISHDataSet.getTCCongNoPhaiThu_isTonTai, )
        If Me.TCCongNoPhaiTraTableAdapter.getTCCongNoPhaiTra_isTonTai(Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma")) = 1 Then
            Me.TCCongNoPhaiTraTableAdapter.Update(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, 0, 0, _
                                                  nguyente, thanhtienvnd, 0, 0, 0, 0, taikhoanno, "", kyketoan, "", ma, getGuid(ma, 4), ma, getGuid(ma, 4), 0)
        Else
            Me.TCCongNoPhaiTraTableAdapter.Insert(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, 0, 0, _
                                                 nguyente, thanhtienvnd, 0, 0, 0, 0, taikhoanno, "", kyketoan, "", ma, getGuid(ma, 4))
        End If
    End Sub
    Private Sub setTonKho()
        Try
            Dim sochungtu As String = SochungtuITextEdit.EditValue
            Dim ma As String = Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma").ToString
            Dim ngaychungtu As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
            Dim madoituong As String = Me.MadoituongISearchLookupEdit.EditValue
            Dim tendoituong As String = Me.MadoituongISearchLookupEdit.Text
            Dim diachidoituong As String = Me.DiachiITextEdit.EditValue
            Dim sohoadon As String = Me.SohoadonITextEdit.EditValue
            Dim ngayhoadon As Date = CDate(Me.NgayhoadonIDateEdit.EditValue)
            Dim hanthanhtoan As Date = CDate(NgaythanhtoanIDateEdit.EditValue)
            'Dim loaitien As String = Me.LoaitienISearchLookupEdit.EditValue
            'Dim tygia As Decimal = CDec(Me.TygianganhangITextEdit.EditValue)
            'Dim nguyente As Decimal = CDec(Me.NguyenteITextEdit.EditValue)
            'Dim thanhtienvnd As Decimal = CDec(Me.ThanhtienvndITextEdit.EditValue)
            Dim taikhoanno As String = Me.PCV_FISHDataSet.getDMNghiepVu_MuaHangTrongNuoc.Rows(Me.GetDMNghiepVuMuaHangTrongNuocBindingSource.Find("ma", Me.ManghiepvuISearchLookupEdit.EditValue)).Item("taikhoanno").ToString
            Dim kyketoan As String = CDate(NgaychungtuIDateEdit.EditValue).Year & Format(CDate(NgaychungtuIDateEdit.EditValue).Month, "00")
            For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                Dim uid As System.Guid = Me.IGridViewHangHoa.GetRowCellValue(index, "uid")
                Dim mahang As String = Me.IGridViewHangHoa.GetRowCellValue(index, "mahang")
                Dim tenhang As String = Me.IGridViewHangHoa.GetRowCellValue(index, "tenhang")
                Dim quicach As String = Me.IGridViewHangHoa.GetRowCellValue(index, "quicach")
                Dim dvt As String = Me.IGridViewHangHoa.GetRowCellValue(index, "dvt")
                Dim solo As String = Me.IGridViewHangHoa.GetRowCellValue(index, "solo")
                Dim sologoc As String = Me.IGridViewHangHoa.GetRowCellValue(index, "sologoc")
                Dim hsd As Date = CDate(Me.IGridViewHangHoa.GetRowCellValue(index, "hsd"))
                Dim soluongnhap As Decimal = Me.IGridViewHangHoa.GetRowCellValue(index, "soluonggiaodich")
                Dim thanhtiennhap As Decimal = Me.IGridViewHangHoa.GetRowCellValue(index, "thanhtienvnd")
                Dim mafifo As String = mahang & solo & Format(hsd, "ddMMyyyy")
                If TCTonKhoTableAdapter.getTCTonKho_isTonTai(uid) = 1 Then
                    TCTonKhoTableAdapter.Update(sochungtu, ngaychungtu, sohoadon, ngayhoadon, mahang, tenhang, quicach, dvt, solo, sologoc, hsd, 0, 0, soluongnhap, thanhtiennhap, 0, 0, 0, 0, mafifo, kyketoan, "", ma, uid, uid, ma, 0)
                Else
                    TCTonKhoTableAdapter.Insert(sochungtu, ngaychungtu, sohoadon, ngayhoadon, mahang, tenhang, quicach, dvt, solo, sologoc, hsd, 0, 0, soluongnhap, thanhtiennhap, 0, 0, 0, 0, mafifo, kyketoan, "", ma, uid)
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewThue.PostEditor()
            Me.IGridViewThue.UpdateCurrentRow()
            Me.IGridViewThue.CloseEditor()
            Me.IGridViewHangHoa.PostEditor()
            Me.IGridViewHangHoa.UpdateCurrentRow()
            Me.IGridViewHangHoa.CloseEditor()
            Me.KHHangHoaBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.EndEdit()
            Me.FKKHThueKHHangHoaBindingSource.EndEdit()
            'Me.FKTCThueTCThue1BindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        setReadonlyControl(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                        setCongNoPhaiThu()
                        setTonKho()
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                setReadonlyControl(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
                Me.State = 0
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub
    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonlyControl(False)
        Me.State = 2
    End Sub

    Private Sub IGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewHangHoa.CellValueChanged

    End Sub

    Private Sub IGridViewTienChiTiet_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridViewHangHoa.FocusedRowChanged
        Dim currentView As GridView = CType(sender, GridView)
        ' The group row's handle. 
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return

        ' Get group summary values. 
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Text = ""
        ' Iterate through group summaries. 
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            ' Get the summry item's Tag property and summary value. 
            Try
                'Text += sumItem.Tag.ToString() + ": " + val.ToString() + "\t"
                MessageBox.Show(val.ToString)
            Catch ex As Exception

            End Try

        Next
    End Sub
    Private Sub IGridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewHangHoa.InitNewRow
        Try
            Me.IGridViewHangHoa.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuISearchLookupEdit.EditValue)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuIDateEdit.EditValue))
            Me.IGridViewHangHoa.SetFocusedRowCellValue("soluongDN", 0) 'phân biệt chữ hoa thường
            Me.IGridViewHangHoa.SetFocusedRowCellValue("soluonggiaodich", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("dongiangoaite", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("dongiavnd", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienngoaite", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienvnd", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueGTGT", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueGTGT", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("chiphimua", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueNK", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueNK", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("giavonnhap", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtiennhap", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienxuat", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanno", TaiKhoanNo)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanco", TaiKhoanCo)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanthueGTGT", TaiKhoanThue)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        Me.IGridViewHangHoa.MoveLast()
        Me.IGridViewHangHoa.Focus()
        Me.IGridViewHangHoa.AddNewRow()
        Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
        Me.IGridViewHangHoa.ShowEditor()
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit5_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs)
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub
    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.KHHangHoaBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPrevious.ItemClick
        Me.KHHangHoaBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNext.ItemClick
        Me.KHHangHoaBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLast.ItemClick
        Me.KHHangHoaBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveFirst()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MovePrevious()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveNext()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveLast()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub
    '============= bắt lỗi igridview1 =========================
    Private Sub IGridView1_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles IGridViewHangHoa.ShownEditor
        Dim view As ColumnView = CType(sender, ColumnView)
        view.ActiveEditor.IsModified = True
        If TypeOf Me.IGridViewHangHoa.ActiveEditor Is SearchLookUpEdit And IsDBNull(Me.IGridViewHangHoa.FocusedValue) Then
            DirectCast(IGridViewHangHoa.ActiveEditor, SearchLookUpEdit).ShowPopup()
        End If
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.SochungtuITextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.KHHangHoaBindingSource.RemoveCurrent()
            'Me.TCThueTableAdapter.Update(Me.PCV_FISHDataSet.TCThue)
            Me.setReadonlyControl(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.KHHangHoaBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewHangHoa.GetRowCellValue(Me.IGridViewHangHoa.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewHangHoa.DeleteRow(Me.IGridViewHangHoa.FocusedRowHandle)
            End If
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Me.FKKHHangHoaChiTietKHHangHoaBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần 'hàng hóa' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        'load dữ liệu lại nhưng giữ nguyên phiếu
        Dim position As Integer = Me.KHHangHoaBindingSource.Position
        loadDuLieu()
        Me.KHHangHoaBindingSource.Position = position
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKhachHang_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKhachHang.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("madonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tendonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKheUocVay_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKheUocVay.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("kheuocvay", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewHangHoa.SetFocusedRowCellValue("hopdongtindung", editor.Properties.View.GetRowCellValue(rowHandle, "sohopdongtindung"))
    End Sub

    Private Sub BarButtonItemChuyenDuLieuThue_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemChuyenDuLieuThue.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 2 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế tự động không ?'", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                If Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle) Then
                End If
                Try
                    For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                        If Me.IGridViewHangHoa.GetRowCellValue(index, "taikhoanno").ToString = "133" Then
                            TienThue += CDec(Me.IGridViewHangHoa.GetRowCellValue(index, "tienvnd"))
                        End If
                    Next
                Catch ex As Exception
                End Try
                Me.TongCong = CDec(Me.IGridViewHangHoa.Columns("tienvnd").SummaryItem.SummaryValue)
                Try
                    Me.PhanTramThue = TienThue / TongCong * 100
                Catch ex As Exception
                    Me.PhanTramThue = 0
                    MessageBox.Show(ex.Message) 'loại trừ chia cho 0
                End Try
                Me.DoanhThu = TongCong - TienThue
                Me.IGridViewThue.AddNewRow()
                'bỏ group by
            End If
        End If

        If Me.TabbedControlGroup2.SelectedTabPageIndex = 4 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu 'quản lý vị trí hàng '  không ?'", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                    Me.MaHang = Me.IGridViewHangHoa.GetFocusedRowCellValue("mahang")
                    Me.TenHang = Me.IGridViewHangHoa.GetFocusedRowCellValue("tenhang")
                    Me.DVT = Me.IGridViewHangHoa.GetFocusedRowCellValue("dvt")
                    Me.LoHang = Me.IGridViewHangHoa.GetFocusedRowCellValue("solo")
                    Me.HSD = CDate(Me.IGridViewHangHoa.GetFocusedRowCellValue("hsd"))
                    Me.IGridViewQuanLyViTriHang.AddNewRow()
                Next
            End If
        End If
    End Sub

    Private Sub IGridViewThue_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewThue.InitNewRow
        Me.IGridViewThue.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("manghiepvu", "MHTN")
        Me.IGridViewThue.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuIDateEdit.EditValue))
        '==================================================================
        Me.IGridViewThue.SetFocusedRowCellValue("doanhthu", Me.DoanhThu)
        Me.IGridViewThue.SetFocusedRowCellValue("phantramthue", Me.PhanTramThue) 'phân biệt chữ hoa thường
        Me.IGridViewThue.SetFocusedRowCellValue("tienthue", Me.TienThue)
        Me.IGridViewThue.SetFocusedRowCellValue("tongcong", Me.TongCong)
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit_HangHoa_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEdit_HangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub

    Private Sub IGridViewQuanLyViTriHang_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewQuanLyViTriHang.InitNewRow
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("mahang", Me.MaHang)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("tenhang", Me.TenHang)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("lohang", Me.LoHang)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("dvt", DVT) 'phân biệt chữ hoa thường
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("hsd", Me.HSD)
    End Sub

    Private Sub MadoituongISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MadoituongISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MadoituongISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.DiachiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
    End Sub

    Private Sub ManghiepvuISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles ManghiepvuISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.ManghiepvuISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TaiKhoanNo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanno")
        Me.TaiKhoanCo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanco")
        Me.TaiKhoanThue = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanvat")
    End Sub

    Private Sub TinhtrangISearchLookupEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TinhtrangISearchLookupEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(0) 'chọn tab hàng hóa
            'Me.BarButtonItemTaoMoiChiTiet.PerformClick
            Me.KHHangHoaBindingSource.EndEdit()
            Me.IGridViewHangHoa.MoveLast()
            Me.IGridViewHangHoa.Focus()
            Me.IGridViewHangHoa.AddNewRow()
            Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
            Me.IGridViewHangHoa.ShowEditor()
        End If
    End Sub

    Private Sub LoaitienISearchLookupEdit_Enter(sender As Object, e As EventArgs) Handles LoaitienISearchLookupEdit.Enter
        If Me.State <> 0 Then
            'Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, CDate(Me.NgaychungtuDateEdit.EditValue))
            Me.DmtygiaBindingSource.Filter = " ngaytygia = '" & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "yyyy-MM-dd") & "'"
        End If
    End Sub

    Private Sub BarButtonItemIn_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItemIn.ItemClick

    End Sub
End Class
